package com.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.eduservice.entity.EduTeacher;
import com.edu.eduservice.mapper.EduTeacherMapper;
import com.edu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.eduservice.vo.TeacherQuery;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-27
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {


    /**
     * 分页条件查询课程
     * @param pageParam
     * @param teacherQuery
     */
    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
            QueryWrapper queryWrapper =new QueryWrapper();
            queryWrapper.orderByAsc("sort");
            if (teacherQuery==null){
                baseMapper.selectPage(pageParam,queryWrapper);
                return;
            }

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified", end);
        }

        queryWrapper.orderByDesc("gmt_create");

        baseMapper.selectPage(pageParam,queryWrapper);

    }



    /**
     * 查询前4热门课程
     * @return
     */
    @Override
    @Cacheable(value = "banner",key = "'findTeacher'")
    public List<EduTeacher> findTeacher() {
        QueryWrapper<EduTeacher> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 4");
        List<EduTeacher> eduTeachers = baseMapper.selectList(wrapper);
        return eduTeachers;
    }



    /**
     * 分页查询讲师
     * @param eduTeacherPage
     * @return
     */
    @Override
    public Map<String, Object> pageListWeb(Page<EduTeacher> eduTeacherPage) {
        QueryWrapper<EduTeacher> queryWrapper= new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        baseMapper.selectPage(eduTeacherPage,queryWrapper);

        List<EduTeacher> records = eduTeacherPage.getRecords();
        //当前页码
        long current = eduTeacherPage.getCurrent();
        long pages = eduTeacherPage.getPages();
        long size = eduTeacherPage.getSize();
        long total = eduTeacherPage.getTotal();

        boolean hasNext = eduTeacherPage.hasNext();

        boolean hasPrevious = eduTeacherPage.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

}
